package com.example.service;

import com.example.entity.CardUse;
import com.example.mapper.CardUseMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
/**
*  使用记录的逻辑层
**/
@Service
@RequiredArgsConstructor
public class CardUseService {

   private final CardUseMapper cardUseMapper;

   /**
   * 增
   **/
   public void add(CardUse cardUse) {
       cardUse.setTime(LocalDateTime.now());
       cardUse.setUserId(TokenUtils.getCurrentUser().getId());
       cardUseMapper.insert(cardUse);
   }

   /**
   * 删
   **/
   public void deleteById(Integer id) {
       cardUseMapper.deleteById(id);
   }

   /**
   * 批量删除
   **/
   public void deleteBatch(List<Integer> ids) {
       for (Integer id : ids) {
       cardUseMapper.deleteById(id);
       }
   }

   /**
   * 改
   **/
   public void updateById(CardUse cardUse) {
       cardUseMapper.updateById(cardUse);
   }

   /**
   * 根据ID查询
   **/
   public CardUse selectById(Integer id) {
       return cardUseMapper.selectById(id);
   }

   /**
   * 查询所有
   **/
   public List<CardUse> selectAll(CardUse cardUse) {
       return cardUseMapper.selectAll(cardUse);
   }

   /**
   * 分页查询
   **/
   public PageInfo<CardUse> selectPage(CardUse cardUse, Integer pageNum, Integer pageSize) {
       PageHelper.startPage(pageNum, pageSize);
       List<CardUse> list = cardUseMapper.selectAll(cardUse);
       return PageInfo.of(list);
   }

}